<?php

namespace common\models\user;

use Yii;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id 用户ID
 * @property string $username 帐号名
 * @property string $nickname 昵称
 * @property string $mobile 手机号
 * @property string $email 邮箱
 * @property string $avatar 头像
 * @property int $gender 性别:0未知，1男，2女
 * @property string $mobile_hash 加密手机号
 * @property string $auth_key 加密密钥
 * @property string $password_hash HASH密码
 * @property string $password_reset_token 重置密码时效Token
 * @property int $ip 注册IP
 * @property string $realname 姓名
 * @property int $company_id 隶属单位ID
 * @property int $admin_id 管理员ID
 * @property int $status 状态
 * @property int $created_at 注册时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 注销时间
 */
class User extends \common\base\User {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['username', 'auth_key'], 'required'],
            [['gender', 'ip', 'company_id', 'admin_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['username', 'nickname', 'mobile', 'auth_key', 'realname'], 'string', 'max' => 32],
            [['email'], 'string', 'max' => 50],
            [['avatar', 'mobile_hash', 'password_hash', 'password_reset_token'], 'string', 'max' => 255],
            [['username'], 'unique'],
            [['mobile'], 'unique'],
            [['email'], 'unique'],
            [['password_reset_token'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => '用户ID',
            'username' => '帐号名',
            'nickname' => '昵称',
            'mobile' => '手机号',
            'email' => '邮箱',
            'avatar' => '头像',
            'gender' => '性别:0未知，1男，2女',
            'mobile_hash' => '加密手机号',
            'auth_key' => '加密密钥',
            'password_hash' => 'HASH密码',
            'password_reset_token' => '重置密码时效Token',
            'ip' => '注册IP',
            'realname' => '姓名',
            'company_id' => '隶属单位ID',
            'admin_id' => '管理员ID',
            'status' => '状态',
            'created_at' => '注册时间',
            'updated_at' => '更新时间',
            'deleted_at' => '注销时间',
            'maskMobile' => '手机号',
            'isAdminer' => '是否管理员',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getAdminer() {
        return $this->hasOne(\common\models\admin\AdminUser::class, ['id' => 'admin_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getStaff() {
        return $this->hasOne(\common\models\work\Staff::class, ['user_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getIsStaff() {
        return $this->getStaff()->exists();
    }

    /**
     * {@inheritdoc}
     */
    public function getIsAdminer() {
        return boolval($this->admin_id);
    }

    /**
     * {@inheritdoc}
     */
    public function getCompany() {
        return $this->hasOne(\common\models\survey\Company::class, ['id' => 'company_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCompanyName() {
        $company = $this->company;
        if (!$company) {
            return '-';
        }
        return $company->name;
    }

    /**
     * {@inheritdoc}
     */
    public function getParentCompanyName() {
        $company = $this->company;
        if (!$company) {
            return '-';
        }
        $cityCompany = $company->parent;
        if (!$cityCompany) {
            return null;
        }
        return $cityCompany->name;
    }

    /**
     * {@inheritdoc}
     */
    public function getCompanyNames() {
        $company = $this->company;
        if (!$company) {
            return [];
        }
        $cityCompany = $company->parent;
        if (!$cityCompany || $cityCompany->name == $company->name) {
            return [$company->name];
        }
        return [
            $cityCompany->name,
            $company->name,
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getMaskMobile() {
        return $this->decryptMobile();
    }

    /**
     * {@inheritdoc}
     */
    public function getFullMobile() {
        return $this->decryptMobile(false);
    }

    /**
     * {@inheritdoc}
     */
    public function getCanGrantAdmin() {
        if ($this->mobile && !$this->admin_id && $this->isStaff) {
            return true;
        }
        return false;
    }

}
